﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using RestSharp;
using System.Windows.Navigation;
using System.Threading;
using UltiDrive.SkyDrive;

namespace UltiDrive
{
    /// <summary>
    /// Interaction logic for LoginPage.xaml
    /// </summary>
    public partial class LoginPage : Window
    {
        private delegate void LogInDelegate();

        public static StorageServices Site;
        public static bool SigningIn;

        public LoginPage()
        {
            InitializeComponent();
            LoginBrowser.LoadCompleted += new LoadCompletedEventHandler(LoginBrowser_LoadCompleted);

            if (SigningIn)
            {
                if (Site == StorageServices.SkyDrive)
                {
                    LoginBrowser.Dispatcher.BeginInvoke(new LogInDelegate(SkyDriveLogIn), null);
                }
            }
        }

        private void SkyDriveLogIn()
        {
            RestClient client = new RestClient("https://login.live.com/");

            RestRequest request = new RestRequest(Method.GET);
            request.Resource = "oauth20_authorize.srf";
            request.AddParameter("client_id", SkyDriveLogIn.SkyDriveClientID);
            request.AddParameter("scope", "wl.signin wl.offline_access");
            request.AddParameter("response_type", "code");
            request.AddParameter("redirect_uri", "https://login.live.com/oauth20_desktop.srf");

            IRestResponse response = client.Execute(request);
            LoginBrowser.Navigate(client.BuildUri(request));
        }

        //need to complete a second authorization
        private void SkyDriveLogInPart2()
        {
            RestClient client = new RestClient("https://login.live.com/");

            RestRequest request = new RestRequest(Method.GET);
            request.Resource = "oauth20_token.srf";
            request.AddParameter("client_id", SkyDriveLogIn.SkyDriveClientID);
            request.AddParameter("client_secret", SkyDriveLogIn.SkyDriveClientSecret);
            request.AddParameter("code", Application.Current.Resources["SkyDriveAuthCode"]);
            request.AddParameter("redirect_uri", "https://login.live.com/oauth20_desktop.srf");
            request.AddParameter("grant_type", "authorization_code");

            IRestResponse<RefreshInfo> test = client.Execute<RefreshInfo>(request);
            this.Close();
        }

        void LoginBrowser_LoadCompleted(object sender, NavigationEventArgs e)
        {
            WebBrowser browser = (WebBrowser)sender;

            string source = browser.Source.ToString();
            if (browser.Source != null)
            {
                if (Site == StorageServices.SkyDrive)
                {
                    //need to send the second authorization
                    if (source.Contains("code") && !source.Contains("client_id"))
                    {
                        Application.Current.Resources["SkyDriveAuthCode"] = Utilities.ParseSourceForIdentifier("code", browser.Source.Query);
                        SkyDriveLogInPart2();
                    }
                }
            }
        }        
    }
}
